package com.qianya.oa.service.impl;


import com.qianya.oa.common.core.domain.entity.SysUser;
import com.qianya.oa.common.core.text.Convert;
import com.qianya.oa.domain.Outside;
import com.qianya.oa.vo.OutsideVo;
import com.qianya.oa.mapper.OutsideMapper;
import com.qianya.oa.service.IOutsideService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 【 出差】Service业务层处理
 * 
 * @author qianya
 * @date 2023-05-30
 */
@Service
public class OutsideServiceImpl implements IOutsideService
{
    @Autowired
    private OutsideMapper outsideMapper;
    @Resource
    IdentityService identityService;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private TaskService taskService;
    /**
     * 查询【 出差】
     * 
     * @param id 【 出差】主键
     * @return 【 出差】
     */
    @Override
    public OutsideVo selectOutsideById(Long id)
    {
        OutsideVo s = outsideMapper.selectOutsideById(id);
        if (s.getPlaneFlag()!=null && s.getPlaneFlag()==1){
            s.setPlaneFlagString("是");
        }else{
            s.setPlaneFlagString("否");
        }
        if (s.getAgreeFlag()!=null && s.getAgreeFlag() == 1){
            s.setAgreeFlagString("是");
        }else{
            s.setAgreeFlagString("否");
        }
        return s;
    }

    /**
     * 查询【 出差】列表
     * 
     * @param outside 【 出差】
     * @return 【 出差】
     */
    @Override
    public List<OutsideVo> selectOutsideList(Outside outside)
    {
        List<OutsideVo> outsideVos = outsideMapper.selectOutsideList2(outside);
        outsideVos.forEach(s->{
            if (s.getPlaneFlag()!=null && s.getPlaneFlag()==1){
                s.setPlaneFlagString("是");
            }else{
                s.setPlaneFlagString("否");
            }
            if (s.getAgreeFlag()!=null && s.getAgreeFlag() == 1){
                s.setAgreeFlagString("是");
            }else{
                s.setAgreeFlagString("否");
            }
        });
        return outsideVos;
    }

    /**
     * 新增【 出差】
     * 
     * @param outside 【 出差】
     * @return 结果
     */
    @Override
    public int insertOutside(Outside outside, SysUser user) {
        outside.setCreateTime(new Date());
        int i = outsideMapper.insertOutside(outside);
        identityService.setAuthenticatedUserId(user.getLoginName());
        HashMap<String, Object> variables = new HashMap<>();
        variables.put("outsideuserid", user.getLoginName());
        variables.put("deptleader", outside.getDeptLeader());
        variables.put("createTime", outside.getCreateTime());
        variables.put("manage", outside.getFinance());
        runtimeService.startProcessInstanceByKey("outside", String.valueOf(outside.getId()), variables);
        // 自动完成第一个任务
        Task autoTask = taskService.createTaskQuery().processDefinitionKey("outside").processInstanceBusinessKey(String.valueOf(outside.getId())).singleResult();
        taskService.complete(autoTask.getId());
        return i;
    }

    /**
     * 修改【 出差】
     * 
     * @param outside 【 出差】
     * @return 结果
     */
    @Override
    public int updateOutside(Outside outside)
    {
        return outsideMapper.updateOutside(outside);
    }

    /**
     * 批量删除【 出差】
     * 
     * @param ids 需要删除的【 出差】主键
     * @return 结果
     */
    @Override
    public int deleteOutsideByIds(String ids)
    {
        return outsideMapper.deleteOutsideByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除【 出差】信息
     * 
     * @param id 【 出差】主键
     * @return 结果
     */
    @Override
    public int deleteOutsideById(Long id)
    {
        return outsideMapper.deleteOutsideById(id);
    }
}
